﻿/*
 * Copyright © 2020, Wuxi Safover Intelligent Technology Co., Ltd. All rights reserved.
 * 
 * 无锡安永智能科技有限公司版权所有，未经许可，不得用于商业用途
 * 
 * Visit: https://www.safover.com/ 
 */

using System;
using System.Diagnostics;
using System.IO.Ports;
using System.Threading;
using Safover.LidarCollector.Core;

namespace Om.Default
{
    public class VirtualNJOpticalMaserDevice : OpticalMaserDevice
    {
        #region Singleton
        public static OpticalMaserDevice Instance { get; } = new VirtualNJOpticalMaserDevice();

        private VirtualNJOpticalMaserDevice() { }
        #endregion

        bool IsOpen { get; set; }

        public override bool IsSerialPortOpen => IsOpen;

        public override string DeviceType => "VirtualNJOpticalMaserDevice";

        protected override bool DoCloseSerialPort()
        {
            IsOpen = false;
            return true;
        }

        public override void ErrorReset()
        {
            Debug.WriteLine($"ErrorReset done.");
        }

        protected override void DoOpenSerialPort()
        {
            Thread.Sleep(50);

            IsOpen = true;
        }

        public override bool StartLasar()
        {
            Debug.WriteLine($"Lasar is on.");

            DeviceReadingsUpdated?.Invoke(this, new OpticalMaserReadingsUpdatedEventArgs
            {
                IsLasarOpen = true,
                InternalTriggerFrequency = 8.88,
                ElectricityRealValue = 9.99
            });

            return true;
        }

        public override bool StopLasar()
        {
            Debug.WriteLine($"Lasar is off.");

            DeviceReadingsUpdated?.Invoke(this, new OpticalMaserReadingsUpdatedEventArgs
            {
                IsLasarOpen = false,
                InternalTriggerFrequency = 6.66,
                ElectricityRealValue = 5.55
            });
            return true;
        }

        protected override void OnDataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            throw new NotImplementedException();
        }
    }
}
